#! /usr/bin/perl

#-------------------------------------------------------------------------------
#--
#-- Module      :  DepConstructor
#-- Copyright   :  (c) Lakshmi Narasimhan T V
#-- License     :  GNU GPLv3
#--
#-- Maintainer  :  <lakshminaras2002 at gmail dot com>
#-- Stability   :  provisional
#-- Portability :  portable
#--
#--
#--
#---------------------------------------------------------------------------------

# This script executes the py file to get the list of packages
# Then does a fedpkg clone to pull in all the package repos

use strict;
use warnings;

my $PROG_DIR;

BEGIN
{
my $TEMP_DIR = File::Basename::dirname($0);
$PROG_DIR = abs_path($TEMP_DIR);
push @INC,$PROG_DIR;
}

use Utils;
use BRFinder;
use OptionsReader;

my $GIT_CLONE_COMMAND="fedpkg clone %1";
my $tmpdir = "/tmp";

sub get_package_list()
{
  print_v("Reading package list");
  my @temp_pkgs = `python $PROG_DIR/checkout-packages.py`;
  if(length @temp_pkgs == 0)
  {
    print_v("No packages to work on");
    # This is fatal so makes sense to exit here
    exit_failure("Error occured in downloading package list");
  }
  my @pkgs = ();
  foreach my $pkg (@temp_pkgs)
  {	
    $pkg = stripws($pkg);
    push @pkgs,$pkg;
  }
  return @pkgs;
}

sub dep_constructor($)
{
  my @pkgs = get_package_list();
  my $reusedirs = shift;
  my @cloned_dirs = ();
  my %pkg_dep_map = ();
  my %specfile_paths = ();

  # Get the location of the package directory
  my $packagedir = get_packagedir();
  if(!-e $packagedir || !-r $packagedir || !-w $packagedir)
  {
    print_v("Unable to read/write into $packagedir"); 
    print_v("Will use $tmpdir");
    $packagedir = $tmpdir;
  }

  if(!$reusedirs)
  {
    # Cleand tmpdir of older repos
    print_v("Removing older directories...");
    foreach my $pkg (@pkgs)
    {
      my $cloned_dir = $packagedir."/".$pkg;
      system("rm -rf $cloned_dir");
    }
  }

  foreach my $pkg (@pkgs)
  {
    $pkg = stripws($pkg);
    if(!defined $pkg || $pkg eq '')
    {
      next;
    }
    print $pkg;
    my $command = $GIT_CLONE_COMMAND;
    $command =~ s/%1/$pkg/;
    my $cloned_dir =  "$packagedir/$pkg";
    if($reusedirs)
    {
      print_v("Reusing directory at $cloned_dir");
    } 
    else
    {
      print_v("Cloing $pkg into $packagedir...");
      my $ret = system("cd $packagedir && $command");
    }
    # Sanity check
    if(!-r $cloned_dir)
    { 
      exit_failure("Unable to read directory $cloned_dir..Exiting");
    }
    push @cloned_dirs,$cloned_dir;
  }
  
  if($#cloned_dirs < 0 )
  {
    print_v("No directories to consider..");
    return %pkg_dep_map;
  }
  
  print @cloned_dirs;

  foreach my $cloned_dir (@cloned_dirs)
  {
    print_v("Processing spec file at $cloned_dir");
    my $specfile = `cd $cloned_dir && ls *.spec`;
    if(!(defined $specfile) || $specfile eq '')
    {
       print_v("No spec file found at $cloned_dir");
       next;
    }
    $specfile = stripws($specfile);
    my $specfilename = $specfile;
    #Strip the .spec from the filename
    $specfilename =~ s/\.spec//g;
    $specfile_paths{$specfilename} = "$cloned_dir/$specfile";
  }

  foreach my $key (keys %specfile_paths)
  {
    my @deps = getbuildrequires($specfile_paths{$key});
    my $specfile_path = $specfile_paths{$key};
    my @pair = (\$specfile_path, \@deps);
    $pkg_dep_map{$key} = \@pair;
  }
  return %pkg_dep_map;
}

1;
